package com.mobiusws.reports.action;

import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.mobiusws.reports.dao.DailyFraudDetailDao;
import com.mobiusws.reports.dao.DailyFraudSummaryDao;
import com.mobiusws.reports.dao.TestDao;
import com.mobiusws.reports.model.DailyFraudDetail;
import com.mobiusws.reports.model.DailyFraudSummary;
import com.mobiusws.reports.model.LoginInfo;
import com.mobiusws.util.JsonUtil;
import com.mobiusws.util.MessageUtil;
import com.mobiusws.util.OperatorConstants;
import com.mobiusws.util.ExportManage;
import com.mobiusws.util.ExportReport;
import com.opensymphony.xwork2.ActionSupport;

public class DailyFraudDetailAction extends ActionSupport {

	private static final long serialVersionUID = 5395964004991248282L;

	@Resource
	private DailyFraudDetailDao dailyFraudDetailDao;
	@Resource
	private JsonUtil jsonUtil;
	@Resource
	private MessageUtil messageUtil;

	private String result;

	private String selDate;
	private int start;
	private int limit;
	private int total;
	
	private String trafficDate;
	private long msisdn;
	private String imsi;
	
	private String fileName;
	private String bQuery; 
	private String suffix;
	
	public String getSuffix() {
		return suffix;
	}

	public void setSuffix(String suffix) {
		this.suffix = suffix;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getBQuery() {
		return bQuery;
	}

	public void setBQuery(String bQuery) {
		this.bQuery = bQuery;
	}

	public String getSelDate() {
		return selDate;
	}

	public void setSelDate(String selDate) {
		this.selDate = selDate;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	// a getter is required to get the result in pages
	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}
	
	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}
	
//	public String getTrafficDate() {
//		return trafficDate;
//	}
//
//	public void setTrafficDate(String trafficDate) {
//		this.trafficDate = trafficDate;
//	}

	public long getMsisdn() {
		return msisdn;
	}

	public void setMsisdn(long msisdn) {
		this.msisdn = msisdn;
	}

	public String getImsi() {
		return imsi;
	}

	public void setImsi(String imsi) {
		this.imsi = imsi;
	}
	
	public String beforeExecute() throws Exception {
		return "page";
	}

	@Override
	public String execute() throws Exception {
		// Thread.sleep(1000);
//		System.out.printf("selDate=%s, start=%s, limit=%s\n",
//				this.getSelDate(), this.getStart(), this.getLimit());
		List<DailyFraudDetail> list = dailyFraudDetailDao
				.getDailyFraudDetailByMSISDN(this);
		this.result = jsonUtil.getJsonByList(list, this.getTotal());
		// System.out.println(this.result);
		ServletActionContext.getResponse().setContentType(
				"text/plain;charset=utf-8");
		PrintWriter out = ServletActionContext.getResponse().getWriter();
		out.print(this.result);
		out.flush();
		out.close();
		return SUCCESS;
	}
	
	public String export() throws Exception {
		if(isMaxExceeded(total)) {
			this.bQuery="E";
		} else {
			this.bQuery="C";
			this.fileName = createExportFiles()+"."+this.suffix;
		}
		return "export";
	}
	
	private boolean isMaxExceeded(int total) {
		String maxSizeStr=messageUtil.getString("max.out.size");
		int maxSize=Integer.parseInt(maxSizeStr);
		return total>maxSize;
	}
	
	private String createExportFiles() {
		ExportManage exportManage = new ExportManage();
		ExportReport reportStruct = new ExportReport();
		
		String[] reportTitleArray = {"Traffic Date","Traffic Hour","Source","MSISDN","IMSI","Cell ID","LAC","IMEI","Vendor","Model","Illegal Odds 7 days","Illegal Odds 24 hrs","Calls Out","B Count","B Ratio","Num Out Adjusted","B2 Ratio","Avg Duration","Off Peak","Bursting","Cells","Calls In","SMS Out","SMS In","CLIR","Audit","Verify","Seconds","Units","Possible Loss","Identified Date","Identified Hour","New","Cdr Time First","Cdr Time Last","Trunk In","Trunk Out"};
		String[] reportColumnsArray = {"traffic_date","traffic_hour","source","msisdn","imsi","cell_id","lac","imei","vendor","model","illegal_odds_7_days","illegal_odds_24_hours","calls_out","b_count","b_ratio","b2_out","b2_ratio","avg_dur","off_peak","bursting","cells","calls_in","sms_out","sms_in","clir","audit","verify","seconds","units","possible_loss","identified_date","identified_hour","new","cdr_time_first","cdr_time_last","trunk_in","trunk_out"};
		reportStruct.setReportTitle("Traffic Date,Traffic Hour,Source,MSISDN,IMSI,Cell ID,LAC,IMEI,Vendor,Model,Illegal Odds 7 days,Illegal Odds 24 hrs,Calls Out,B Count,B Ratio,Num Out Adjusted,B2 Ratio,Avg Duration,Off Peak,Bursting,Cells,Calls In,SMS Out,SMS In,CLIR,Audit,Verify,Seconds,Units,Possible Loss,Identified Date,Identified Hour,New,Cdr Time First,Cdr Time Last,Trunk In,Trunk Out");
		reportStruct.setReportTitleArray(reportTitleArray);
		reportStruct.setTableColumnsArray(reportColumnsArray);
		reportStruct.setReportName("Hourly_" + getMsisdn());
		String datetime=selDate.replaceAll("-", "");
		reportStruct.setReportDate(datetime);
		reportStruct.setTableName("daily_fraud_detail_report");
		
		long maskBits = OperatorConstants.MASK_VALUE;
		String outputPath = "/opt/GK2/portal/querydata";
		exportManage.DBQueryReportDaily(OperatorConstants.MCC+"_"+OperatorConstants.MNC, "0", maskBits, reportStruct, outputPath, dailyFraudDetailDao.getQueryList(this));
		
		return reportStruct.getReportName()+"_"+reportStruct.getReportDate();
	}

}
